﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;

public partial class SYS_POPEDOM_Role_Manage : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //if (!Ext.IsAjaxRequest)
            {
                //BindData();
            }
            AddTabForMaster();
        } 
    }

    private void AddTabForMaster()
    {
        string Url = G_Url_allstring + "/SYS_POPEDOM/Role_Create.aspx";
        String tabid = Request.QueryString["tabid"];
        extBtn_Add.Listeners.Click.Handler = "popfortab(GridPanel1,'" + Url + "','ObjId','Add',0,'','" + tabid + "','用户角色增加',1)";
        extBtn_Edit.Listeners.Click.Handler = "popfortab(GridPanel1,'" + Url + "','ObjId','ROLE_ID',0,'','" + tabid + "','用户角色编辑',2)";
        GridPanel1.Listeners.RowDblClick.Handler = "popfortab(GridPanel1,'" + Url + "','ObjId','ROLE_ID',rowIndex,'','" + tabid + "','用户角色编辑',3)";

    }
    protected void OnStoreRefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindData();
    }
    private void BindData()
    {
        DataTable dtTmp = CommonLib.BLL.SYS_ROLES.GetList("creator IN (SELECT su.[USER_ID] FROM SYS_USER su WHERE Charindex(RTRIM('" + BS_LoginDeptId + "'),su.DEPT_ID,0) > 0 )").Tables[0];
        if (dtTmp.Rows.Count > 0)
        {
            //绑定数据
            this.Store1.DataSource = dtTmp;
            this.Store1.DataBind();
            //SelectFristData(GridPanel1, dtTmp);
        }
    }
    protected void extBtn_Refresh_Click(object sender, DirectEventArgs e)
    {
        //BindData();
        GridPanel1.Reload();
    }
    protected void extBtn_Delete_Click(object sender, DirectEventArgs e)
    {
        int nCount = 0;
        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
        nCount = sm.SelectedRows.Count;
        if (nCount == 0)
        {
            X.Msg.Alert("系统提示", "请先选择要删除的记录！").Show();
            return;
        }

        X.Msg.Confirm("系统提示", "是否删除选中行?", new MessageBoxButtonsConfig
        {
            Yes = new MessageBoxButtonConfig
            {
                Handler = "Ext.net.DirectMethods.DoDelMenu();",
                Text = "是"
            },
            No = new MessageBoxButtonConfig
            {
                Text = "否"
            }
        }).Show();

    }

    //删除菜单
    [DirectMethod]
    public void DoDelMenu()
    {
        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
        string recordIDs = "";
        foreach (SelectedRow row in sm.SelectedRows)
        {
            if (row.RecordID.Trim() == "01" || row.RecordID.Trim() == "02")
            {
                X.Msg.Alert("系统提示", "删除记录失败！</br>选择项中包含系统内置角色，不允许删除").Show();
                return;
            }
            recordIDs = recordIDs + row.RecordID.Trim() + ";";
        }
        try
        {
            if (CommonLib.BLL.SYS_ROLES.DeleteTrans(recordIDs) == 0)
            {
                X.Msg.Alert("系统提示", "删除记录失败！").Show();
                return;
            }
            else
                GridPanel1.Reload();
        }
        catch
        {
            X.Msg.Alert("系统提示", "删除记录失败！").Show();
            return;
        }
    }
}
